package cn.changeforyou.web.dao.mybatis.ext.extBuilder;

import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class DaoSpringHolder implements ApplicationContextAware {
    static boolean mapUnderscoreToCamelCase;

    private static ApplicationContext APPLICATION_CONTEXT;
    /**
     * 这里只从数据库取数据是否下划线转驼峰法, 等同于数据库是下划线命名, java是驼峰法命名
     * @return
     */
    public static boolean isMapUnderscoreToCamelCase() {
        return mapUnderscoreToCamelCase;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        APPLICATION_CONTEXT = applicationContext;
        mapUnderscoreToCamelCase = applicationContext.getBean(SqlSessionFactory.class).getConfiguration().isMapUnderscoreToCamelCase();
    }

    public static <T> T getBean(Class<T> tClass){
        return APPLICATION_CONTEXT.getBean(tClass);
    }
}
